<?php
namespace Home\Controller;

use Think\Controller;

class DrController extends Controller
{

    public function _initialize()
    {
        if (empty($_SESSION['empid'])) {
            
            $this->error('页面加载中。。。', U('Login/Login'), 1);
        }
    }

    public function Upload()
    {
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = 3145728; // 设置附件上传大小
        $upload->exts = array(
            'xls',
            'xlsx'
        ); // 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
                                     // 上传文件
        $info = $upload->upload();
//        header("Content-type:text/html;charset=utf-8");
//        dump($info);exit;
        if (! $info) { // 上传错误提示错误信息
            $this->error($upload->getError());
        } else { // 上传成功
            //相当于$_GET['fs'];
            $fs = I('get.fs');
            //R方法调用某个控制器的方法，一般用于跨模块调用
            $s = R('Excel/upload', array(
                "./Uploads/" . $info['photo']['savepath'] . $info['photo']['savename'],
                $info['photo']['ext'],
                $fs
            ));
//            header("Content-type:text/html;charset=utf-8");
//            var_dump($s);exit;
            if ($fs == 'px') {
                $teacher = R('Pub/Teacher');
                $classlb = R('Pub/Classlb');
                $this->assign('classlb', $classlb);
                $this->assign('teacher', $teacher);
                
                $this->assign("data", $s);
                
                $bcbh = $this->creatbc();
                
                $this->assign('bcbh', $bcbh);
                
                $this->display('peixunadmin');
            } elseif ($fs == 'xy') {
                
                $bcbh = I('post.bcbh');
                $this->assign("data", $s);
                // var_dump($s);
                // session('data',$s);
                // $data=session('data');
                // print_r($data);
                $this->assign('bcbh', $bcbh);
                $this->display('xueyuanadmin');
            }
        }
    }

    public function peixunadmin()
    {
        $this->display();
    }

    public function add()
    {
        $map['scheid'] = I('post.bcbh');
        $sum = M('CSchedule')->where($map)->count();
        if ($sum > 0) {
            
            $data = 2;
        } else {
            //判断老师是否存在老师表
            $teacherList = M('CTeacher')->field('tname')->select();
            $nameArr = array(); //name列
            foreach ($teacherList as $key => $value) {
                $nameArr[] = $value['tname'];
            }
            $isin = in_array(I('post.jteacher'),$nameArr);
            $isin2 = in_array(I('post.dteacher'),$nameArr);
            //判断班次类别是否正确
            $dictList = M('SysDict')->field('DICT_NAME')->select();
//            print_r($dictList);exit;
            $dictArr = array(); //name列
            foreach ($dictList as $key => $value) {
                $dictArr[] = $value['dict_name'];
            }
//            print_r($dictArr);exit;
            $isin3 = in_array(I('post.classlb'),$dictArr);
            if($isin == false){
                echo "无此接班老师，请注意名字是否正确";exit;
            }else if($isin2 == false){
                echo "无此带班老师，请注意名字是否正确";exit;
            }else if($isin3 == false){
                echo "无此班次类别，请注意名字是否正确";exit;
            }
                $data['scheid'] = I('post.bcbh');
                $data['schename'] = I('post.bcmc');
                $data['jteacher'] = I('post.jteacher');
                $data['dteacher'] = I('post.dteacher');
                $data['classlb'] = I('post.classlb');
                $data['zbdw'] = I('post.zbdw');
                $data['crc'] = "系统导入";
                $clas = M('CSchedule')->add($data);
                $class = M('Xxb');
                $class->create();
                $add = $class->add();
                if ($add) {
                    $data = 0;
                } else {
                    $data = 1;
                }


        }
        
        echo $data;
    }

    public function Peixunlist()
    {
        $list = M('Xxb')->order('bcbh desc')->select();
        $this->assign('list', $list);
        $this->display();
    }

    public function del()
    {
        $map['id'] = I('get.id');
        $lb = I('get.lb');
        if ($lb = 'Peixun') {
            $del = M('Xxb')->where($map)->delete();
        }
        
        if ($del) {
            
            $this->success('删除成功');
        } else {
            
            $this->success('删除失败，请检查');
        }
    }

    public function Peixunxg()
    {
        if (IS_AJAX) {
            if (I('post.obcbh') != I('post.bcbh')) {
                $map['scheid'] = I('post.bcbh');
                $sum = M('CSchedule')->where($map)->count();
            } else {
                
                $sum = 0;
            }
            if ($sum > 0) {
                
                $data = 2;
            } else {
                $class = M('Xxb');
                
                $class->create();
                $add = $class->save();
                if ($add) {
                    $map['scheid'] = I('post.obcbh');
                    $data['scheid'] = I('post.bcbh');
                    $data['schename'] = I('post.bcmc');
                    $data['jteacher'] = I('post.jteacher');
                    $data['dteacher'] = I('post.dteacher');
                    $data['classlb'] = I('post.classlb');
                    $clas = M('CSchedule')->where($map)->save($data);
                    $data = 0;
                } else {
                    $data = 1;
                }
            }
            
            echo $data;
        } else {
            $map['id'] = I('get.id');
            $data = M('Xxb')->where($map)->find();
            $this->assign('data', $data);
            $teacher = R('Pub/Teacher');
            $classlb = R('Pub/Classlb');
            $this->assign('classlb', $classlb);
            $this->assign('teacher', $teacher);
            $this->display();
        }
    }

    public function Xueyuanadmin()
    {
        if (IS_POST) {
            $class = M('CStudent');
            $mn['classid'] = I('post.classid2');
            $count = $class->where($mn)->count();
            if ($count > 0) {
                
                $del = $class->where($mn)->delete();
            }
            
            for ($i = 0; $i < count($_POST["loginname"]); $i ++) {
                
                $data['loginname'] = $_POST['loginname'][$i];
                $data['classid'] = $_POST['classid'][$i];
                $data['sex'] = $_POST['sex'][$i];
                $data['nation'] = $_POST['nation'][$i];
                $data['Company'] = $_POST['Company'][$i];
                $data['Political'] = $_POST['Political'][$i];
                $data['telephone'] = $_POST['telephone'][$i];
                $data['IDcard'] = $_POST['IDcard'][$i];
                $data['room'] = $_POST['room'][$i];
                $data['car'] = $_POST['car'][$i];
				$data['level'] = $_POST['level'][$i];
                $data['remark'] = $_POST['remark'][$i];
                $add = $class->add($data);
            }
            if ($add) {
                $data['md'] = 2;
                $map['bcbh'] = $_POST['classid2'];
                $sa = M('Xxb')->where($map)->save($data);
                
                $data = 0;
            } else {
                
                $data = 1;
            }
            
            echo $data;
        } else {
            $bcbh = I('get.bcbh');
            $this->assign('bcbh', $bcbh);
            
            $this->display();
        }
    }

    public function peixunone()
    {
        if (IS_AJAX) {
            $map['classid'] = I('post.bcbh');
            $data = M('CStudent')->where($map)->select();
            $this->assign('data1', $data);
            $this->display();
        }
    }

    public function biaodan()
    {
        if (IS_AJAX) {
            $map['bcbh'] = I('post.bcbh');
            
            $data = M('Xxb')->where($map)->find();
            
            $this->assign('data', $data);
            $this->display();
        }
    }

    public function creatbc()
    {
        $sj = date('YmdH');
        $sjs = rand(0, 999999);
        $t = $sj . $sjs;
        
        return $t;
    }

    //模板下砸
    public function TempletDownload(){
        header("Content-type:text/html;charset=utf-8");
        $strFilePath = "Public/moban/123.xlsx";
//        print_r($strFilePath);exit;
        if (!file_exists($strFilePath)) {
            echo "File not found";
            exit();
        }

        $strSaveFileName = "培训信息表.xlsx";
        $strSaveFileName = iconv("utf-8","gb2312",$strSaveFileName);
        //统一去空格
//        $strFilePath = str_replace(" ", "", $strFilePath);
//        ob_clean();
        $fp = fopen($strFilePath,"r");
        $file_size = filesize($strFilePath);
        //下载文件需要用到的头
        Header("Content-type: application/octet-stream");
        Header("Accept-Ranges: bytes");
        Header("Accept-Length:".$file_size);
        Header("Content-Disposition: attachment; filename=".$strSaveFileName);
        $buffer = 1024;
        $file_count = 0;
        //向浏览器返回数据
        while(!feof($fp) && $file_count < $file_size){
            $file_con = fread($fp,$buffer);
            $file_count += $buffer;
            echo $file_con;
        }
        fclose($fp);
        exit;

    }
}
